package utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * To generate a time string.
 * @author 1991sfg@gmail.com
 * @date 2013-12-19
 */
public class DateTimeUtils {

    /**
     * 生成当前系统时间对应的字符串
     * 格式为yyyy-mm-dd hh:MM:ss
     * @return String
     */
    public static String generateDateTimeString() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    
    /**
     * 生成当前系统时间对应的时间戳
     * @return Timestamp
     */
    public static Timestamp generateTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }
    
    /**
     * 生成stamp对应的字符串
     * @param stamp 时间戳
     * @return String
     */
    public static String generateDateTimeString(Timestamp stamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(stamp);
    }
    
    /**
     * 生成文件名称
     * @return
     */
    public static String generateFilenameString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");   
        return df.format(new Date()) + "_"  
                + new Random().nextInt(1000);
    }
    /**
     * 生成订单
     * @return
     */
    public static String generateOrderNumString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");   
        return df.format(new Date()) + new Random().nextInt(1000);
    }
    /**
     * 生成时间字符串
     * @param date
     * @return
     */
    public static String generateDateTimeString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    
    /**
     * 生成日期字符串
     * @param date
     * @return
     */
    public static String generateDateString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
    
    /**
     * 生成Date对象
     * @param str
     * @return
     */
    public static Date generateDateTime(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            return new Date();
        }
    }
    
    /**
     * 生成Date对象
     * @param str
     * @param format
     * @return
     */
    public static Date generateDateTime(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            return new Date();
        }
    }
    
    /**
     * 获取指定域
     * @param d
     * @param format
     * @return
     */
    public static int get(Date d, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return Integer.parseInt(sdf.format(d));
    }

    /**
     * 生成Date对象（yyyy-MM-dd）
     * @param string
     * @return
     */
    public static Date generateDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            return new Date();
        }
    }
    
    /**
     * 格式化时间字符串
     * @param time
     * @return
     */
    public static String customDateTimeString(String time) {
        if (time == null) {
            return "时间处理错误";
        }
        String custom;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = sdf.parse(time);
            Date now = new Date();
            long diff = now.getTime() - date.getTime();
            if (diff < 0) {
                custom = sdf2.format(date);
            } else if (diff < 60000) {
                custom = ((int)diff / 1000) + "秒前";
            } else if (diff < 3600000) {
                custom = ((int)diff / 60000) + "分钟前";
            } else if (diff < 43200000) {
                custom = ((int)diff / 3600000) + "小时前";
            } else {
                custom = sdf2.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
            custom = "时间处理错误";
        }
        return custom;
    }
    
    /**
     * 格式化时间字符串
     * @param time
     * @return
     */
    public static String customDateTimeToDate(String time) {
        if (time == null) {
            return "时间处理错误";
        }
        String custom;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(time);
            custom = sdf2.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            custom = "时间处理错误";
        }
        return custom;
    }

    public static String generateDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    
}
